console.log("加载汉化模块");

//1.汉化杂项
var cnItems = {
    _OTHER_: [],
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    '': '',
    //原路返回
    'a': 'a',
    '0/': '0/',
    '1 x': '1 x',
    '1.': '1.',
    '(D4)': '(D4)',
    '(D6)': '(D6)',
    '(P)': '(P)',
    '⇄': '⇄',
    '⇅': '⇅',
    '+': '+',
    '-20%;': '-20%;',
    '(1/17)': '(1/17)',
    '(2/17)': '(2/17)',
    '(3/17)': '(3/17)',
    '(4/17)': '(4/17)',
    '(5/17)': '(5/17)',
    '(6/17)': '(6/17)',
    '(7/17)': '(7/17)',
    '(8/17)': '(8/17)',
    '(9/17)': '(9/17)',
    '(10/17)': '(10/17)',
    '(11/17)': '(11/17)',
    '(12/17)': '(12/17)',
    '(13/17)': '(13/17)',
    '(14/17)': '(14/17)',
    '(15/17)': '(15/17)',
    '(16/17)': '(16/17)',
    '(17/17)': '(17/17)',
    '(B)': '(B)',
    '(B) ': '(B) ',


}

//需处理的前缀
var cnPrefix = {
}

//需处理的后缀
var cnPostfix = {
    ' ':' '
}

//需排除的，正则匹配
var cnExcludeWhole= [
    /^[x+\-]?\d+(\.\d+)?(e[+\-]?\d+)?(\%)?\s*$/,                                          //12.34e+4
    /^\s*$/,                                                                //纯空格
];
var cnExcludePostfix = [
    /:?\s*x?\d+(\.\d+)?(e[+\-]?\d+)?\s*$/,                                          //12.34e+4
    /:?\s*x?\d+(\.\d+)?[A-Za-z]{0,2}$/,  //: 12.34K, x1.5
]

//正则替换，带数字的固定格式句子
var cnRegReplace = new Map([
	[/^You were gone for (\d+) Seconds.$/, '你离开了 $1 秒。'],
	[/^(\d+) Royal points$/, '$1 皇家点数'],
	[/^Cost: (\d+) RP$/, '成本：$1 皇家点数'],
	[/^Usages: (\d+)\/$/, '用途：$1\/'],
	[/^workers: (\d+)\/$/, '工人：$1\/'],

]);

//2.采集新词
//20190320@JAR

var cnItem = function () {

    //传参是否非空字串
    if (!arguments[0]) return;

    //检验传参是否对象
    let text = arguments[0],
        s = '';
    if (typeof (text) != "string")
        return text;
    else
        s = arguments[0].charCodeAt();

    //检验传参是否英文
    // if (
    //     s < 65 || (s > 90 && s < 97) || (s > 122)
    //
    // ) return text;

    //处理前缀
    let text_prefix = "";
    for(let prefix in cnPrefix){
        if (text.substr(0,prefix.length) === prefix){
            text_prefix = cnPrefix[prefix];
            text = text.substr(prefix.length);
        }
    }
    //处理后缀
    let text_postfix = "";
    for(let postfix in cnPostfix){
        if (text.substr(-postfix.length) === postfix){
            text_postfix = cnPostfix[postfix];
            text = text.substr(0,text.length-postfix.length);
        }
    }
    //处理正则后缀
    let text_reg_exclude_postfix = "";
    for(let reg of cnExcludePostfix){
        let result = text.match(reg);
        if (result){
            text_reg_exclude_postfix = result[0];
            text = text.substr(0, text.length-text_reg_exclude_postfix.length);
        }
    }

    //检验字典是否可存
    if (!cnItems._OTHER_) cnItems._OTHER_ = [];

    //检查是否排除
    for(let reg of cnExcludeWhole){
        if (reg.test(text)){
            return text_prefix + text + text_reg_exclude_postfix + text_postfix;
        }
    }
    
    //尝试正则替换
    for (let [key, value] of cnRegReplace.entries()) {
        if (key.test(text)) {
            return text_prefix + text.replace(key, value) + text_reg_exclude_postfix + text_postfix;
        }
    }

    //遍历尝试匹配
    for (let i in cnItems) {
        //字典已有词汇或译文、且译文不为空，则返回译文
        if (
            text == i || text == cnItems[i] &&
            cnItems[i] != ''
        )
            return text_prefix + cnItems[i] + text_reg_exclude_postfix + text_postfix;
    }

    //调整收录的词条，0=收录原文，1=收录去除前后缀的文本
    let save_cfg = 1;
    let save_text = save_cfg?text:arguments[0]
    //遍历生词表是否收录
    for (
        let i = 0; i < cnItems._OTHER_.length; i++
    ) {
        //已收录则直接返回
        if (save_text == cnItems._OTHER_[i])
            return arguments[0];
    }

    if (cnItems._OTHER_.length < 500){
        //未收录则保存
        cnItems._OTHER_.push(save_text);
        cnItems._OTHER_.sort(
            function (a, b) {
                return a.localeCompare(b)
            }
        );
    }

    /*
        //开启生词打印
        //console.log(
            '有需要汉化的英文：', text
        );
    */

    //返回生词字串
    return arguments[0];
};

//document.createElement = function(name,opt){
//	if(name=='canvas'){
//		let canvasElement = document.__proto__.createElement.bind(this)(name,opt);
//		canvasElement.getContext('2d').fillText = function(text,x,y,max){
//			return this.__proto__.fillText.bind(this)(cnItem(text),x,y,max);
//		}
//		canvasElement.getContext('2d').strokeText = function(text,x,y,max){
//			return this.__proto__.strokeText.bind(this)(cnItem(text),x,y,max);
//		}
//		canvasElement.getContext('2d').measureText = function(text){
//			return this.__proto__.measureText.bind(this)(cnItem(text));
//		}
//		return canvasElement;
//	}
//	else
//		return document.__proto__.createElement.bind(this)(name,opt);
//}
